package kassiopeia.gui

import javax.jms.Message;
import org.apache.log4j.Logger;
import kassiopeia.commons.log.Log;
import kassiopeia.commons.util.JmsUtils;
import kassiopeia.gui.utils.LogsBuffer;
import grails.plugin.jms.Subscriber;

class LogsService {

    private static final Logger log = Logger.getLogger(LogsService.class)

    static transactional = true

    static exposes = ["jms"]

    private LogsBuffer logBuffer = new LogsBuffer(200)

    @Subscriber(topic = 'KASSIOPEIA_LOGS')
    def incomingLog(Message message) {
        Log logMessage = JmsUtils.convertObjectMessage(message, Log.class)

        if(logMessage == null) {
            log.warn('Unknow message type -> ignoring incoming JMS message')
            return
        }
        logBuffer.offer(logMessage)
        return null
    }

    def getLogsStartingFrom(logId) {
        return logBuffer.pollFrom(logId)
    }

    def getAllLogs() {
        return logBuffer.pollAll()
    }
}
